package com.gwtplatform.samples.hplace.client.productlist;

import com.google.gwt.user.client.ui.HasHTML;
import com.google.inject.Inject;

import com.guit.client.GuitPresenter;
import com.guit.client.View;
import com.guit.client.async.AbstractAsyncCallback;
import com.guit.client.binder.GuitBinder;
import com.guit.client.binder.ViewField;
import com.guit.client.binder.ViewHandler;
import com.guit.client.place.Place;
import com.gwtplatform.samples.hplace.client.HasInnerHTML;
import com.gwtplatform.samples.hplace.client.breadcrumbs.Backeable;
import com.gwtplatform.samples.hplace.client.breadcrumbs.BreadcrumbsPresenter;
import com.gwtplatform.samples.hplace.client.product.ProductPresenter;
import com.gwtplatform.samples.hplace.client.productlist.ProductListPresenter.ProductListBinder;
import com.gwtplatform.samples.hplace.shared.GetProductList;
import com.gwtplatform.samples.hplace.shared.GetProductListResult;
import com.gwtplatform.samples.hplace.shared.Product;

import java.util.ArrayList;
import java.util.List;

public class ProductListPresenter extends GuitPresenter<ProductListBinder> implements Place<Backeable<Integer>> {

    public static interface ProductListBinder extends GuitBinder<ProductListPresenter> {
    }
    
    @Inject
    public ProductListPresenter(BreadcrumbsPresenter breadcrumbsPresenter) {
        this.breadcrumbsPresenter = breadcrumbsPresenter;
    }
    
    BreadcrumbsPresenter breadcrumbsPresenter;
    private Backeable<Integer> backeable;
    
    @ViewField
    HasInnerHTML title;

    @ViewField
    HasHTML products;
    
    public void setList(List<Product> products) {
        StringBuilder sb = new StringBuilder();
        for (Product product : products) {
            if (sb.length() > 0) {
                sb.append("<br/>");
            }
            
            sb.append("<a href='#");
            ArrayList<String> names = new ArrayList<String>(backeable.getNames());
            names.add(title.getInnerHTML());
            ArrayList<String> tokens = new ArrayList<String>(backeable.getBackTokens());
            tokens.add(placeManager.getCurrentToken());
            sb.append(placeManager.getToken(ProductPresenter.class, new Backeable<Integer>(product.getId(), tokens, names)));
            sb.append("'>");
            sb.append(product.getName());
            sb.append("</a>");
        }
        this.products.setHTML(sb.toString());
    }

    private String getTitleFor(int type) {
        if (type == Product.FLAG_FAVORITE) {
            return "Favorite products";
        } else if (type == Product.FLAG_SPECIAL) {
            return "Specials";
        } else {
            return "All products";
        }
    }
    
    @ViewHandler
    protected void back$click() {
        ArrayList<String> backTokens = backeable.getBackTokens();
        placeManager.go(backTokens.get(backTokens.size() - 1));
    }

    @Override
    public void go(final Backeable<Integer> backeable) {
        this.backeable = backeable;
        commandService.execute(new GetProductList(backeable.getValue()), new AbstractAsyncCallback<GetProductListResult>() {
            @Override
            public void success(GetProductListResult result) {
                View view = getView();
                breadcrumbsPresenter.buildNavegation(title.getInnerHTML(), view, backeable);
                title.setInnerHTML(getTitleFor(backeable.getValue()));
                setList(result.getProducts());
            }
        });
    }
}
